<?php /*a:1:{s:63:"H:\xampp\htdocs\www\lqb\application\login\view\login\login.html";i:1563842112;}*/ ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="stylesheet" type="text/css" href="/static/login/css/normalize.css" />
    <link rel="stylesheet" type="text/css" href="/static/login/css/demo.css" />
    <!--必要样式-->
    <link rel="stylesheet" type="text/css" href="/static/login/css/component.css" />
    <script type="text/javascript" src="/static/admin/layui/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="/static/admin/layui/layui.js"></script>
    <link rel="stylesheet" type="text/css" href="/static/admin/layui/css/layui.css" />
</head>
<body>
<div class="container demo-1">
    <div class="content">
        <div id="large-header" class="large-header">
            <canvas id="demo-canvas"></canvas>
            <div class="logo_box">
                <h3>推广后台</h3>
                <form action="#" name="f" method="post">
                    <div class="input_outer">
                        <span class="u_user"></span>
                        <input name="username" class="text" style="color: #FFFFFF !important" type="text" placeholder="请输入账户">
                    </div>
                    <div class="input_outer">
                        <span class="us_uer"></span>
                        <input name="password" class="text" style="color: #FFFFFF !important; position:absolute; z-index:100;"value="" type="password" placeholder="请输入密码">
                    </div>

                    <div class="input_outer" style="width: 180px">
                        <span class="u_yzm"></span>
                        <input name="captcha" class="text" style="color: #FFFFFF !important; position:absolute; z-index:100;"value="" type="text" placeholder="请输入验证码">
                        <img style="height: 41px; padding-left: 180px; border-radius:50px" src="<?php echo url('common/Verify/yzm'); ?>" alt="captcha" onclick="this.src='/common/Verify/yzm'" />
                    </div>
                    <div class="mb2"><a id = "sub" lay-filter="sub" class="act-but submit" href="javascript:;" style="color: #FFFFFF">登录</a></div>
                </form>
            </div>
        </div>
    </div>
</div>
<!-- /container -->
<script type="text/javascript" src="/static/login/js/TweenLite.min.js"></script>
<script type="text/javascript" src="/static/login/js/EasePack.min.js"></script>
<script type="text/javascript" src="/static/login/js/rAF.js"></script>
<script type="text/javascript" src="/static/login/js/demo-1.js"></script>
</body>
<script>
    //加载弹出层组件
    layui.use('layer',function(){
        var layer = layui.layer;
        // 获取token
        var token = layui.data('cache');
        if(token !== 'undefined'){
            $.post({
                url:"/api/v1.Token/verifyToken",
                type:"post",
                headers:{"token":token.token},
                dataType:"json",
                success:function (res) {
                    if(res.code == 200){
                        window.location.href = "/login/Show/index"
                    }
                }
            })
        }
        //登录的点击事件
        $("#sub").on("click",function(){
            login();
        })

        //登录函数
        function login(){
            var username = $(" input[ name='username' ] ").val();
            var password = $(" input[ name='password' ] ").val();
            var captcha = $(" input[ name='captcha' ] ").val();


            // 先在本地查找token值
            var tokenValue = layui.data('cache');
            // 再携带token去服务器判断token是否失效
            if(tokenValue.token == 'undefined'){
                $.ajax({
                    url:"/api/v1.Token/verifyToken",
                    data:{"token":tokenValue.token},
                    type:"post",
                    dataType:"json",
                    success:function(res){
                        // 200则token不失效
                        if(res.code == 200){
                            console.log("token没失效");
                            window.location.href = "/login/Show/index"
                        }
                        // 404则token失效
                        if(res.code == 404){
                            $.ajax({
                                url:"/login/Login/checkNamePwd",
                                data:{"username":username,"password":password, "captcha": captcha},
                                type:"post",
                                dataType:"json",
                                success:function(res){
                                    console.log(res);
                                    if(res.code == 200){
                                        layer.msg("成功");
                                        // 先判断本地token是否存在，存在则删除本地的token
                                        if(tokenValue.token){
                                            layui.data('cache', {
                                                key: 'token'
                                                ,remove: true
                                            });
                                        }
                                        // 在本地新建token
                                        layui.data('cache', {
                                            key: 'token'
                                            ,value: res.token
                                        });
                                        window.location.href = "/login/Show/index"
                                    }
                                },
                                error:function(error){
                                    layer.msg("失败");
                                },
                            })
                        }
                    }
                })
            }else {
                $.ajax({
                    url:"/login/Login/checkNamePwd",
                    data:{"username":username,"password":password, "captcha": captcha},
                    type:"post",
                    dataType:"json",
                    success:function(res){
                        console.log(res);
                        if(res.code == 200){
                            layer.msg("登陆成功"+ username, {
                                time: 2500,
                            });
                            // 先判断本地token是否存在，存在则删除本地的token
                            if(tokenValue.token){
                                layui.data('cache', {
                                    key: 'tokenValue.token'
                                    ,remove: true
                                });
                            }
                            // 在本地新建token
                            layui.data('cache', {
                                key: 'token'
                                ,value: res.token
                            });
                            window.location.href = "/login/Show/index"
                        }
                    },
                    error:function(error){
                        var msg = error.responseJSON.msg
                        layer.msg(JSON.stringify(msg));
                    },
                })
            }
        }
    })
</script>
</html>